﻿package com.snakedickgames.common.sandy 
{
    import com.pblabs.engine.debug.Logger;
    import com.pblabs.engine.entity.PropertyReference;
    import com.snakedickgames.common.sandy.SandyNode;
    import sandy.core.scenegraph.ATransformable;
    import sandy.core.data.Point3D;
    import flash.geom.Point;
    
    
    public class SandyTransformable extends SandyNode
    {
        public var positionProperty:PropertyReference;
        public var positionXYProperty:PropertyReference;
        public var yawProperty:PropertyReference;
        
        public function set yaw(value:Number):void
        {
            var t:ATransformable = transformable;
            if (t)
            {
                t.rotateY = value;
            }
        }
        
        public function get transformable():ATransformable
        {
            return _node as ATransformable;
        }
        
        public function set position(pos:Point3D):void
        {
            var tf:ATransformable = transformable;
            if (!tf)
                return;
            tf.x = pos.x;
            tf.y = pos.y;
            tf.z = pos.z;
        }
        
        override public function onFrame(deltaTime:Number):void 
        {
            super.onFrame(deltaTime);
            
            var propPos:Point3D;
            if (positionProperty)
            {
                propPos = owner.getProperty(positionProperty) as Point3D;
                position = propPos;
            }
            else if (positionXYProperty)
            {
                var pos2d:Point = owner.getProperty(positionXYProperty) as Point;
                propPos = new Point3D(pos2d.x, pos2d.y, transformable.z);
            }
            if (propPos)
                position = propPos;
            
            if (yawProperty)
            {
                var rotation:Number = owner.getProperty(yawProperty) as Number;
                yaw = rotation;
            }
        }
    }
}